package com.qo.questiononline.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qo.questiononline.mapper.QlTestpaperMapper;
import com.qo.questiononline.model.QlPaperAnswer;
import com.qo.questiononline.model.QlPaperDetail;
import com.qo.questiononline.model.QlTestpaper;
import com.qo.questiononline.model.TablePaperDetail;
import com.qo.questiononline.utils.ResMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * api
 *
 * @author eq
 * @version 1.0.0
 */
@Slf4j
@Controller
@RequestMapping("/api")
public class TestPaperController {

    /**
     * 试卷操作
     */
    private QlTestpaperMapper qlTestpaperMapper;

    @Autowired
    public void setQlTestpaperMapper(QlTestpaperMapper qlTestpaperMapper) {
        this.qlTestpaperMapper = qlTestpaperMapper;
    }

    /**
     * 获取试卷
     *
     * @param request     请求
     * @param qlTestpaper 查询条件
     * @return {Json} Json字符串
     */
    @RequestMapping("paper")
    @ResponseBody
    public String getPaper(HttpServletRequest request, @RequestBody QlTestpaper qlTestpaper) {
        ResMessage resMessage = new ResMessage();
        HttpSession session = request.getSession(); // 获取浏览器Session
        String userInfo = (String) session.getAttribute("userInfo"); // 取的userInfo中的用户信息
        String users[] = userInfo.split(",");
        qlTestpaper.setCreatedBy(users[1]);
        List<QlTestpaper> qlTestpaperList = qlTestpaperMapper.getPaper(qlTestpaper);
        return resMessage.sysSuccess(qlTestpaperList);
    }

    /**
     * 根据用户及状态查询答卷
     *
     * @param request       请求
     * @param qlPaperAnswer 条件
     * @return JSON字符串
     */
    @RequestMapping("paperByStatus")
    @ResponseBody
    public String getPaperByStatus(HttpServletRequest request, @RequestBody QlPaperAnswer qlPaperAnswer) {
        ResMessage resMessage = new ResMessage();
        HttpSession session = request.getSession(); // 获取浏览器Session
        String userInfo = (String) session.getAttribute("userInfo"); // 取的userInfo中的用户信息
        String users[] = userInfo.split(",");
        qlPaperAnswer.setCreatedBy(users[1]);
        List<QlPaperAnswer> qlPaperAnswerList = qlTestpaperMapper.getPaperByPeople(qlPaperAnswer);
        return resMessage.sysSuccess(qlPaperAnswerList);
    }

    /**
     * 获取试卷详细信息
     *
     * @param qlPaperDetail 条件
     * @return JSON对象
     */
    @RequestMapping("pub/paperDetail")
    @ResponseBody
    public String paperDetail(@RequestBody QlPaperDetail qlPaperDetail) {
        ResMessage resMessage = new ResMessage();
        List<QlPaperDetail> qlPaperDetailList = qlTestpaperMapper.getPaperDetail(qlPaperDetail);
        return resMessage.sysSuccess(qlPaperDetailList);
    }

    /**
     * 答卷
     *
     * @param request 请求
     * @param answer  答案
     * @return JSON对象
     */
    @RequestMapping("upDateAnswer")
    @ResponseBody
    public String upDateAnswer(HttpServletRequest request, @RequestBody Map<String, Object> answer) {
        ResMessage resMessage = new ResMessage();
        QlPaperDetail qlPaperDetail = new QlPaperDetail();
        for (Map.Entry<String, Object> entry : answer.entrySet()) {
            String mapKey = entry.getKey().intern();
            qlPaperDetail.setId(Integer.parseInt(mapKey));
            String mapValue = entry.getValue().toString();
            qlPaperDetail.setAnswer(mapValue);
            qlTestpaperMapper.upDateAnswer(qlPaperDetail);
        }
        return resMessage.sysSuccess();
    }

    @RequestMapping("createPaper")
    @ResponseBody
    public String createPaper(HttpServletRequest request, @RequestBody JSONObject data) {
        // 请求头信息
        HttpSession session = request.getSession(); // 获取浏览器Session
        String userInfo = (String) session.getAttribute("userInfo"); // 取的userInfo中的用户信息
        String users[] = userInfo.split(",");
        // 时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 创建试卷
        QlTestpaper qlTestpaper = new QlTestpaper();
        JSONObject paper = data.getJSONObject("testPaper");
        log.info("{}-{}-{}-{}", paper.getString("title"), paper.getString("describe"), paper.getString("deadLine"), paper.getString("answerTime"));
        qlTestpaper.setTitle(paper.getString("title"));
        qlTestpaper.setDescribe(paper.getString("describe"));
        qlTestpaper.setDeadline(paper.getString("deadLine"));
        qlTestpaper.setAnswerTime(Integer.parseInt(paper.getString("answerTime")));
        qlTestpaper.setCreatedBy(users[1]);
        qlTestpaper.setStatus(1);
        qlTestpaper.setCreatedTime(simpleDateFormat.format(date));
        int influence = qlTestpaperMapper.addPaper(qlTestpaper); // 创建试卷
        long paperId = qlTestpaper.getId();
        // 创建试卷问题
        JSONArray questions = data.getJSONArray("questions");
        TablePaperDetail tablePaperDetail = new TablePaperDetail();
        tablePaperDetail.setPaper(paperId);
        for (Object question : questions) {
            tablePaperDetail.setQuestion(Integer.parseInt(question.toString()));
            qlTestpaperMapper.addPaperDetail(tablePaperDetail);
        }

        ResMessage resMessage = new ResMessage();
        return resMessage.sysSuccess();
    }
}
